import com.mysql.cj.jdbc.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: wx
 * Date: 2023-11-13
 * Time: 16:17
 */
public class Testjdbc {
    //创建数据源,描述你要操作的数据源,在哪里.
    public static void main(String[] args) throws SQLException {


        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/newsql?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("1234");

        //和数据库服务器建立连接
        Connection connection = dataSource.getConnection();

        //构建sql
        String sql = "insert into student values(104,18,20003,'张三',0009)";
          //需要吧String SQL 转成 语句对象
        PreparedStatement statement = connection.prepareStatement(sql);


        //把构造好的 sql 发送给服务器去执行
        int n = statement.executeUpdate();
        System.out.println("n = "+ n);

        //释放必要资源
        statement.close();
        connection.close();
    }

}
